package com.ztesoft.sca.dao;

import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Component;

import com.ztesoft.sca.model.AudioSourceDto;
import com.ztesoft.sca.model.DataSetModel;
import com.ztesoft.sca.model.QcSceneModel;
import com.ztesoft.sca.model.ScaAudioResult;
import com.ztesoft.sca.model.TaskAudioRelaModel;
import com.ztesoft.sca.model.TaskModel;

import oracle.sql.CLOB;

@Component
public class TaskDaoImpl implements TaskDao{
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public TaskDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

	@Override
	public void addTask(TaskModel aiTask) {
		// TODO Auto-generated method stub
        StringBuffer sqlBuf = new StringBuffer("insert into SCA_TASK " +
                "" +
                "  (TASK_ID, TASK_NAME,SET_ID,AREA_ID,SCENE_ID,START_TIME,STATUS,ALITASKID)" +
                "" +
                "VALUES" +
                "" +
                "  (?, ?,?, ?,?, ?,?, ?)");
        Object[] param = new Object[]{aiTask.getTaskId(),aiTask.getTaskName(),aiTask.getSetId(),aiTask.getAreaId(),aiTask.getSceneId(),aiTask.getStartTime(),aiTask.getStatus(),aiTask.getAlitaskid()};
        this.jdbcTemplate.update(sqlBuf.toString(),param);
	}

	@Override
	public List<TaskModel> selectAichatTaskList(String receiveStatus) {
        String sqlStr="select TASK_ID,ALITASKID from SCA_TASK where STATUS=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{receiveStatus};
        List<TaskModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(TaskModel.class));
        return list;
	}

	@Override
	public void updateAiChatTaskInStr(List<Long> listId) {
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("listId", listId);
        String sql = "update SCA_TASK set STATUS=1 where TASK_ID in (:listId)";
        namedParameterJdbcTemplate.update(sql, params);
	}

	@Override
	public List<TaskModel> listTask(long id, int from, int size) {
        String sqlStr="SELECT * FROM (SELECT ROWNUM rn,TASK_ID, TASK_NAME,SET_ID,AREA_ID,SCENE_ID,START_TIME,STATUS,ALITASKID "
        		+ "FROM (SELECT TASK_ID, TASK_NAME,SET_ID,AREA_ID,SCENE_ID,START_TIME,STATUS,ALITASKID FROM SCA_TASK)WHERE ROWNUM<=? ORDER BY TASK_ID asc) t2 WHERE t2.rn >=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        if (from < 0) {
            from = Integer.MAX_VALUE; 
        }
        Object[] param = new Object[]{size,from};
        List<TaskModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(TaskModel.class));
        return list;
	}

	@Override
	public List<TaskModel> detailTask(TaskModel aiChat) {
        String sqlStr="SELECT TASK_ID, TASK_NAME,SET_ID,AREA_ID,SCENE_ID,START_TIME,STATUS,ALITASKID FROM SCA_TASK WHERE TASK_ID=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{aiChat.getTaskId()};
        List<TaskModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(TaskModel.class));
        return list;
	}

	@Override
	public List<DataSetModel> jobTabledataSet(Long jobId) {
        String sqlStr="select * from sca_dataset a where a.set_id=(select t.set_id from sca_task t where t.task_id=?)";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{jobId};
        List<DataSetModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(DataSetModel.class));
        return list;
	}

	@Override
	public CLOB jobTableruleCheckResult(Long jobId) {
        StringBuffer sqlBuf =new StringBuffer("select result_content from sca_task_result a where a.task_id=?");
        Object[] param = new Object[]{jobId};
        CLOB list = (CLOB)this.jdbcTemplate.queryForObject(sqlBuf.toString(), param, Clob.class);
        return list;
	}

	@Override
	public List<AudioSourceDto> detailAudioSourceTask(AudioSourceDto aiChatAudio, Long jobId) {
        String sqlStr="select * from sca_audio_source a where a.file_name=? and a.set_id=(select t.set_id from sca_task t where t.task_id=?)";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{aiChatAudio.getFileName(),jobId};
        List<AudioSourceDto> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(AudioSourceDto.class));
        return list;
	}

	@Override
	public List<AudioSourceDto> selectAudioSourceList(AudioSourceDto aiAudio) {
        String sqlStr="select * from sca_audio_source where 1=1";
        Object[] param =new Object[]{};
       
        //status
        if("0".equals(aiAudio.getStatus())||aiAudio.getStatus()=="0"){
        	param = new Object[]{aiAudio.getStatus()};
        	sqlStr=sqlStr+" and status=? ";
        }
        //tid
        if(!"".equals(aiAudio.getFile_Id())&&aiAudio.getFile_Id()!=null){
        	param = new Object[]{aiAudio.getFile_Id()};
        	sqlStr=sqlStr+" and file_id=? ";
        }
        
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        List<AudioSourceDto> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(AudioSourceDto.class));
        return list;
	}

	@Override
	public List<QcSceneModel> selectSceneList(Long sceneId) {
        String sqlStr="select * from sca_scene  where scene_id=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{sceneId};
        List<QcSceneModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(QcSceneModel.class));
        return list;
	}

	@Override
	public void updateAudioSourceTask(List<AudioSourceDto> listUpdate) {
	     NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(listUpdate.toArray());
	   	 String sql = "UPDATE sca_audio_source SET status=:status where file_id=:file_Id";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}

	@Override
	public void addAudioTask(List<TaskAudioRelaModel> listAddAudioTask) {
    	NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(listAddAudioTask.toArray());
	   	 String sql = "INSERT INTO SCA_TASK_AUDIO_RELA(file_id,task_id) "
	   	 		+ "VALUES (:fileId,:taskId)";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
		
	}

	@Override
	public void addAudioResult(List<ScaAudioResult> listAddAudioResult) {
	   	NamedParameterJdbcTemplate namedParameterJdbcTemplateResult = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
		   	 SqlParameterSource[] beanSourcesResult = SqlParameterSourceUtils.createBatch(listAddAudioResult.toArray());
		   	 String sqlResult = "INSERT INTO SCA_AUDIO_RESULT(result_id,file_id) "
		   	 		+ "VALUES (:resultId,:fileId)";
		   	namedParameterJdbcTemplateResult.batchUpdate(sqlResult, beanSourcesResult);
	}

	@Override
	public void updateAudioResult(List<ScaAudioResult> listUpdateAudioResult) {
    	NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(listUpdateAudioResult.toArray());
	   	 String sql = "update SCA_AUDIO_RESULT set book_flag=:bookFlag,is_book=:isBook,sca_book_desc=:scaBookDesc,"
	   	 		+ "handup_flag=:handupFlag,is_handup=:isHandup,sca_handup_desc=:scaHandupDesc where file_id=:fileId ";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}
}
